<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin\Permission;
use App\Models\Admin\Role;
use App\Models\Admin\RolePermission;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RoleController extends Controller
{

    public function index(Request $request)
    {
        if($request->isMethod('post')){
            $post = $request->input('delete');
            Role::destroy($post);
            return redirect()->route('admin.role')->with('status', 'success');
        }else {
            $res = ['title' => '我的'];
            //$res['user'] = Auth::user();
            $res['data'] = Role::Paginate(2);
            return view('admin.role.index', ['res' => $res]);
        }
    }

    public function add(Request $request)
    {
        if($request->isMethod('post')) {
            $role = new Role;
            $post = $role->check($request);
            $role->name = $post['name'];
            $role->user_id = Auth::id();
            if($role->save()){
                return redirect()->route('admin.role')->with('status', 'success');
            }else{
                return back()->with('status', 'fail');
            }

        }else{
            $res=['title'=>'我的'];
            //$res['user']=Auth::user();
            return view('admin.role.add',['res'=>$res]);
        }
    }

    public function edit(Request $request)
    {
        if($request->isMethod('post')) {
            $role = Role::find($request->id);
            $post = $role->check($request);
            $role->name = $post['name'];
            if($role->save()){
                return redirect()->route('admin.role')->with('status', 'success');
            }else{
                return back()->with('status', 'fail');
            }
        }else{
            $res=['title'=>'我的'];
            //$res['user']=Auth::user();
            $res['info'] = Role::find($request->id);
            return view('admin.role.edit',['res'=>$res]);
        }
    }

    public function permission(Request $request)
    {
        if($request->isMethod('post')) {
            $rolePermission = new RolePermission;
            $role = Role::find($request->id);
            if($role){
                $role->permission()->sync($request->input('permission_id'));
            }
            return redirect()->route('admin.role')->with('status', 'success');
        }else{
            $res=['title'=>'我的'];
            //$res['user']=Auth::user();
            $res['info'] = Role::find($request->id);
            $res['rolepermission'] = $res['info']->permission->toArray();
            $res['rolepermission'] = array_column($res['rolepermission'], 'id');
            $res['permission'] = Permission::all()->toArray();
            return view('admin.role.permission',['res'=>$res]);
        }

    }

    public function del(Request $request)
    {
        $role = Role::find($request->id);
        $role->delete();
        return redirect()->route('admin.role')->with('status', 'success');
    }
}
